package messages;

import java.io.IOException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import utils.Serialization;
import utils.XmlSerializationUtils;
import connection.exception.ConnectionResetException;

public class CompetenceValidationRequestMessage implements Convertible {

    private static final long serialVersionUID = 1L;

    public String _competence;

    public Integer _nonce;

    public String _signature;

    public CompetenceValidationRequestMessage() {}
    
    public CompetenceValidationRequestMessage(String competence, Integer nonce, String signature) {
        _competence = competence;
        _nonce = nonce;
        _signature = signature;
    }
    
    @Override
    public Element convertToCanonic(Document doc) {
        
        String className = getClass().getSimpleName();
        
        Element el = doc.createElement(className);

        if (_competence != null) {
            Element childEl = doc.createElement("Competence");
            childEl.setTextContent(_competence);
            el.appendChild(childEl);
        } 

        Element childEl = doc.createElement("Nonce");
        childEl.setTextContent(_nonce.toString());
        el.appendChild(childEl);

        if (_signature != null) {
            Element childEl3 = doc.createElement("SignedNonce");
            childEl3.setTextContent(_signature);
            el.appendChild(childEl3);
        } 

        return el;
    } 


    @Override
    public Object convertFromCanonic(Element el) {
        this._competence = XmlSerializationUtils.getTextValue(el, "Competence");
        this._nonce = XmlSerializationUtils.getIntValue(el, "Nonce");
        this._signature = XmlSerializationUtils.getTextValue(el, "SignedNonce");
        return this;
    }

    public static void main(String[] args) throws ConnectionResetException, IOException{

        CompetenceValidationRequestMessage gen = new CompetenceValidationRequestMessage();
        gen._competence = "PresidenteIST";
        gen._nonce = 1000;
        gen._signature = "DSADafsfasasDSDASDcsad";

        byte[] serialized = Serialization.Serialize(gen);

        Object newGen = Serialization.Deserialize(serialized);

        newGen.toString();

        System.in.read();
    }
}

